0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 function sgwt_demo2
0029 close all
0030 fprintf('Welcome to SGWT demo #2\n');
0031
0032
0033 gb=[]; c=[];
0034
0035
0036 fh=figure('Visible','on','Name','demo 2 ui','Position',[425,920,400,150]);
0037 uipanelh=uipanel('Parent',fh,'Title','','Units','pixels','BorderType','none');
0038 tsliderh=uicontrol(uipanelh,'style','slider','max',50,'min',0,'value',1,...
0039 'sliderstep',[.005 .1],'position',[25,10,300,20],...
0040 'callback',{@tslider_callback});
0041 msliderh=uicontrol(uipanelh,'style','slider','max',100,'min',1,'value',20,...
0042 'sliderstep',[.001 .1],'position',[25,60,300,20],...
0043 'callback',{@mslider_callback});
0044 jbuttonh=uicontrol(uipanelh,'style','pushbutton','position',[50,110,150,20],...
0045 'string','Select center vertex','callback',{@jbutton_callback});
0046 ttexth=uicontrol(uipanelh,'style','text','string','','position',[325,10,100,20]);
0047 mtexth=uicontrol(uipanelh,'style','text','string','','position',[325,60,100,20]);
0048 jtexth=uicontrol(uipanelh,'style','text','string','','position',[325,100,100,20]);
0049 uicontrol(uipanelh,'style','text','string',...
0050 'Chebyshev polynomial order (m)','position',...
0051 [60,80,200,20]);
0052 uicontrol(uipanelh,'style','text','string',...
0053 'Wavelet scale (t)','position',...
0054 [60,30,200,20]);
0055
0056
0057
0058 fprintf('Loading minnesota traffic graph\n');
0059 Q=load('minnesota.mat');
0060 xy=Q.xy;
0061 A=Q.A;
0062 N=size(A,1);
0063 x=xy(:,1);
0064 y=xy(:,2);
0065
0066 fprintf('Computing graph laplacian\n')
0067 [ki,kj]=find(A);
0068 L=sgwt_laplacian(A);
0069 fprintf('Measuring largest eigenvalue, lmax = ');
0070 lmax=sgwt_rough_lmax(L);
0071 fprintf('%g\n',lmax);
0072 arange=[0 lmax];
0073
0074 msize=100;
0075
0076
0077 t=3;
0078
0079 m=20;
0080 jcenter=550;
0081
0082 fprintf('\n');
0083 update_uitext;
0084 update_graphfig
0085 update_kernel
0086 update_waveletfigs
0087
0088 function update_graphfig
0089 figure(2)
0090 set(gcf,'renderer','zbuffer');
0091 fprintf('Displaying traffic graph\n');
0092 set(gcf,'position',[0,600,400,400]);
0093
0094 hold on
0095 scatter(x,y,msize,[.5 .5 .5],'.');
0096 plot([x(ki)';x(kj)'],[y(ki)';y(kj)'],'k');
0097 set(gca,'Xtick',[]);
0098 set(gca,'Ytick',[]);
0099 axis equal
0100 axis off
0101 scatter(x(jcenter),y(jcenter),msize,'r.');
0102 drawnow
0103 end
0104
0105 function update_kernel
0106
0107 t1=1;
0108 t2=2;
0109 a=2;
0110 b=2;
0111 tmin=t1/lmax;
0112
0113
0114 set(tsliderh,'min',tmin);
0115 gb= @(x) sgwt_kernel(x,'a',a,'b',b,'t1',t1,'t2',t2);
0116 g=@(x) gb(t*x);
0117
0118 for k=1:numel(g)
0119 c=sgwt_cheby_coeff(g,m,m+1,arange);
0120 end
0121 lambda=linspace(0,lmax,1e3);
0122 figure(3)
0123 set(gcf,'position',[425,580,600,250])
0124 plot(lambda,g(lambda),lambda,sgwt_cheby_eval(lambda,c,arange));
0125 legend('Exact Wavelet kernel','Chebyshev polynomial approximation');
0126 end
0127
0128 function update_waveletfigs
0129
0130 fprintf('\nReomputing wavelets with t=%g, m=%g\n',t,m);
0131 d=sgwt_delta(N,jcenter);
0132 fprintf('Computing wavelet by naive forward transform\n');
0133 figure(4)
0134 set(gcf,'position',[0,100,400,400])
0135 wp_e=sgwt_ftsd(d,gb,t,L);
0136 show_wavelet(wp_e,x,y);
0137
0138 title('exact wavelet (naive forward transform)');
0139 fprintf('Computing wavelet by Chebyshev approximation\n');
0140 figure(5)
0141 set(gcf,'position',[425,100,400,400])
0142
0143 wp_c=sgwt_cheby_op(d,L,c,arange);
0144 show_wavelet(wp_c,x,y);
0145 title('approximate wavelet (transform via chebyshev approximation)');
0146 relerr=norm(wp_e-wp_c)/norm(wp_e);
0147 fprintf('Relative error between exact and approximate wavelet %g\n',relerr)
0148 end
0149
0150 function show_wavelet(wp,x,y)
0151 [Fs,s_ind]=sort(abs(wp),'descend');
0152 scatter(x(s_ind),y(s_ind),msize,wp(s_ind),'.');
0153 caxis([-1 1]*max(abs(wp)));
0154 hcb=colorbar('location','north');
0155 set(gca,'Xtick',[]);
0156 set(gca,'Ytick',[]);
0157 cxt=get(hcb,'Xtick');
0158 cxt=[cxt(1),0,cxt(end)];
0159 set(hcb,'Xtick',cxt);
0160 cpos=get(hcb,'Position');
0161 cpos(4)=.02;
0162 set(hcb,'Position',cpos);
0163 axis equal
0164 axis off
0165 end
0166
0167 function update_uitext
0168 set(ttexth,'string',sprintf('t=%0.3f',t));
0169 set(mtexth,'string',sprintf('m=%g',m));
0170 set(jtexth,'string',sprintf('j=%g',jcenter));
0171 end
0172
0173 function tslider_callback(source,eventdata)
0174 t=get(tsliderh,'value');
0175 update_uitext;
0176 update_kernel;
0177 update_waveletfigs;
0178 end
0179
0180 function mslider_callback(source,eventdata)
0181 newm=get(msliderh,'value');
0182 if newm<m
0183 m=floor(newm);
0184 else
0185 m=ceil(newm);
0186 end
0187 set(msliderh,'value',m);
0188 update_uitext;
0189 update_kernel;
0190 update_waveletfigs;
0191 end
0192
0193 function jbutton_callback(source,eventdata)
0194 figure(2)
0195 fprintf('Select new center vertex\n');
0196 [xp,yp]=ginput(1);
0197 oldjcenter=jcenter;
0198 jcenter=argmin((xp-x).^2+(yp-y).^2);
0199 scatter(x(jcenter),y(jcenter),msize,'r.');
0200 scatter(x(oldjcenter),y(oldjcenter),msize,[.5 .5 .5],'.');
0201 drawnow
0202 update_uitext
0203 update_waveletfigs
0204 end
0205
0206 end
0207
0208 function i = argmin(x)
0209 i=min(find(x==min(x)));
0210 end